var bridge

window.onload = function() {
    var imgs = document.getElementsByTagName("img");
    for (index in imgs){
        imgs[index].id = "" + index;
        imgs[index].onclick = function() {
            didTappedImage(this.id,imgs);
        };
    }
}

// 图片点击事件
function didTappedImage(index,imgs) {
    var image = imgs[index];
    var width = image.width;
    var height = image.height;
    var x = image.getBoundingClientRect().left;
    var y = image.getBoundingClientRect().top;
    x = x + document.documentElement.scrollLeft;
    y = y + document.documentElement.scrollTop;
    
    var imgSrcs = [];
    for (var i=0;i<imgs.length;i++){
        imgSrcs[i] = imgs[i].src;
    }
    
    bridge.callHandler('showImg', {'x':x ,'y':y, 'width':width,'height':height,'index':index,'imgs':imgSrcs }, function responseCallback(responseData) {
                            console.log("JS received response:", responseData)
                       });
}

// 设置字体
function setFontName(name) {
    var content = document.getElementById('content');
    content.style.fontFamily = name;
}

// 设置字体大小
function setFontSize(size) {
    var content = document.getElementById('content');
    content.style.fontSize = size + "px";
    
    //content.className = 'abc'
}

function setContentClass(className) {
    var content = document.getElementById('content');
    content.className = className;
}

// 获取网页高度
function getHtmlHeight() {
    return document.body.offsetHeight;
}

function setupWebViewJavascriptBridge(callback) {
    if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
    if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
    window.WVJBCallbacks = [callback];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'https://__bridge_loaded__';
    document.documentElement.appendChild(WVJBIframe);
    setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}

setupWebViewJavascriptBridge(function(bridge) {
                             
                             /* Initialize your app here */
                             self.bridge = bridge;
                             
                             bridge.registerHandler('showImg', function(data, responseCallback) {
                                                    alert("showImg");
                                                    })
                             })
